varying vec3 normal;
varying float depth;
varying vec2 uv;
varying vec4 mvPos;

attribute vec3 tangent;

// components of the (inverse) TBN matrix
varying vec3 TBN1;
varying vec3 TBN2;
varying vec3 TBN3;

vec3 TBN(in vec3 by, in vec3 normal, in vec4 Tangent)
{
	vec3 binormal 	= cross(normal, Tangent.xyz) * Tangent.w;

	vec3 tvec;
	tvec.x = dot(Tangent.xyz,  by);
	tvec.y = dot(binormal, by);
	tvec.z = dot(normal,   by);
	return tvec;
}

// inverse TBN - might need optimization
void iTBNComp(in vec3 N, in vec4 T, out vec3 r1, out vec3 r2, out vec3 r3)
{
    vec3 B 	= cross(N, T.xyz) * T.w;
    r1 = vec3(T.x, B.x, N.x);
    r2 = vec3(T.y, B.y, N.y);
    r3 = vec3(T.z, B.z, N.z);
}
void main() {
    normal = normalize(gl_NormalMatrix * gl_Normal);
    
    mvPos = gl_ModelViewMatrix * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    
    uv = gl_MultiTexCoord0.st;
    
    depth = gl_Position.z;
    
    // tangents - HACK: using the color array
    vec4 tangent = gl_Color.xyzw;
    
    TBN1 = vec3(1.0, 0.0, 0.0);
    TBN2 = vec3(0.0, 1.0, 0.0);
    TBN3 = vec3(0.0, 0.0, 1.0);
    if (tangent.w != 0.0) {
        tangent.xyz = normalize(gl_NormalMatrix * tangent.xyz);
        // generate inverse TBN matrix components
        iTBNComp(normal, tangent, TBN1, TBN2, TBN3);
    }
    
    
    
}